define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {


    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'agent/info/index',
                    add_url: 'agent/info/add',
                    edit_url: 'agent/info/edit',
                    del_url: 'agent/info/del',
                    multi_url: 'agent/info/multi',
                    table: 'agent_info',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                 searchFormVisible: true,
                 searchFormTemplate: 'customformtpl',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'agent_name', title: __('Agent_name')},
                        {field: 'agent_phone', title: __('Agent_phone')},
                        {field: 'province', title: __('Addre'), table: table,formatter: Controller.api.formatter.province},
                        {field: 'address', title: __('Address')},
                        {field: 'agent_contact', title: __('Agent_contact')},
                        {field: 'agent_region', title: __('Agent_region')},
                        {field: 'term_start', title: __('Term_start'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,datetimeFormat: 'YYYY-MM-DD'},
                        {field: 'term_end', title: __('Term_end'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime,datetimeFormat: 'YYYY-MM-DD'},
                        {field: 'clerk_text', title: __('Clerk')},
                        {field: 'docking_text', title: __('Docking')},
                        {field: 'bond_amount', title: __('Bond_amount'), operate:'BETWEEN'},
                        {field: 'deduction_amount', title: __('Deduction_amount'), operate:'BETWEEN'},
                     
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $(document).on("fa.event.appendfieldlist", "#add-form .btn-append", function (e, obj) {
              Form.events.selectpage(obj);
            });
            Controller.api.bindevent();
            // Controller.api.selecteds();
        },
        edit: function () {
            $(document).on("fa.event.appendfieldlist", "#edit-form .btn-append", function (e, obj) {
              Form.events.selectpage(obj);
            });
            Controller.api.bindevent();
            Controller.api.selecteds();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            selecteds: function () {
                $("#append").click(function () {
                    console.log($("#fieldlist").find('.hidden').eq(0).length);
                    if($("#fieldlist").find('.hidden').eq(0).length>0){
                        $("#fieldlist").find('.hidden').eq(0).removeClass('hidden');
                    }else{
                        Layer.msg("已经没有分类啦");
                    }
                });
                $(".btn-removes").on('click',function () {
                    var dd = $(this).parent('dd');
                    dd.find('select').val(0);
                    dd.find('select').change();
                    $(dd).addClass('hidden');
                });
               var oldVal="";
               changeSelcet();
                $(document).on('change', ".selet", function () {
                        oldVal=$(this).attr("old");
                        var _thisVal=$(this).find('option:selected').val();
                        var id=$(this).attr("id");
                        $(this).parent().siblings("dd").find("option[value='"+_thisVal+"']").not("option[value=0]").hide();
                        $(this).parent().siblings("dd").find("option[value='"+oldVal+"']").show();
                        $(this).find("option[value='"+oldVal+"']").show();
                        $(this).attr("old",_thisVal);
                        //changeSelcet();
                });
                function changeSelcet(){
                  $('.fieldlist select').each(function() {
                    if ($(this).find("option:selected")) {
                        var _thisVal = $(this).find('option:selected').val();
                         oldVal=$(this).attr("old",_thisVal);
                        $(this).parent().siblings("dd").find("option[value='"+_thisVal+"']").not("option[value=0]").hide();
                        $(this).parent().siblings("dd").find("option[value='"+oldVal+"']").show();
                        $(this).find("option[value='"+oldVal+"']").show();
                         //$('.fieldlist select').parent().siblings("dd").find("option[value='"+_thisVal+"']").not("option[value=0]").hide()
                    }
                   });
                }
            },
            formatter: {//渲染的方法
                 province: function (value, row, index) {
                    var areatext = '';
                    if(value){
                        areatext = Config.arealist[value];
                        if(row['city']){
                             areatext +=' '+Config.arealist[row['city']];
                            if(row['area']){
                                areatext +=' '+Config.arealist[row['area']];
                            }
                        }
                    }
                  return areatext;
                },
               
          },
        }
    };
    return Controller;
});